package com.leetcode.algorithm.y22.m07;

import java.util.ArrayList;
import java.util.List;

/**
 * 228. 汇总区间
 * 
 * https://leetcode.cn/problems/summary-ranges/
 * 
 * @author jie.deng
 *
 */
class Question0228Solution01 {
	
	public List<String> summaryRanges(int[] nums) {
		List<String> retList = new ArrayList<>();
		int len = nums.length;
		if (len == 0) {
			return retList;
		}
		int idx = 0;
		while (idx < len) {
			int fromIdx = idx;
			while (idx < len - 1 && nums[idx + 1] == nums[idx] + 1) {
				idx++;
			}
			int endIdx = ((idx == len - 1) ? len - 1 : idx);
			if (endIdx == fromIdx) {
				retList.add("" + nums[fromIdx]);
			} else {
				retList.add(nums[fromIdx] + "->" + nums[endIdx]);
			}
			idx++;
		}

		return retList;
	}
    
}